package org.lewis.common.jobs;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StartJobListener implements ServletContextListener {
	
	private Log log = LogFactory.getLog(StartJobListener.class);
	
	private  CronTriggerHelper cronTriggerHelper = new CronTriggerHelper();

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		try {
			log.info("StartJobListener contextDestroyed.");
			cronTriggerHelper.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		log.info("StartJobListener contextInitialized.");
		new Thread(new StartJobThread()).start();
	}
	
	public class StartJobThread implements Runnable {
	    public void run() {
	    	 try {
				cronTriggerHelper.run();
			} catch (Exception e) {
				e.printStackTrace();
			}
	    }
	}

}
